<%-- 
    Document   : select_user
    Created on : 20.02.2014, 16:54:01
    Author     : Corvus 11
--%>

<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@page contentType="text/html" pageEncoding="UTF-8" language="java"%>

<tiles:insertDefinition name="baseTemplate">
    
    <tiles:putAttribute name="postLinks">
        <script src="js/dialog-rights.js"></script>
        <script src="js/dialog-register.js"></script>
    </tiles:putAttribute>
    
    <tiles:putAttribute name="title">
        <title>Администрирование</title>
    </tiles:putAttribute>  
        
        <tiles:putAttribute name="selectedItem">
            <input type="hidden" id="menu_selected_item" value="menuitem_select_user">
        </tiles:putAttribute>
    
         <tiles:putAttribute name="body"> 
        <div class="container" id="select_user_container">
            
            <button id="su_new_user" class="button bg-orange fg-white" onclick="dialogNewUser(-1);">
                    <i class="icon-plus on-left"></i>
                    Создать пользователя
            </button>
            <button id="su_block_user" class="button bg-darkRed fg-white" onclick="blockUser();">
                    <i class="icon-cancel on-left"></i>
                    Блокировать пользователя
            </button>
            <button id="su_set_rights" class="button bg-darkBlue fg-white" onclick="dialogSetRights();">
                    <i class="icon-briefcase-2 on-left"></i>
                    Настроить права
            </button>
            
            <hr>
            
            <table id="select_user_table" class="table hovered striped dataTable bordered">
                    <thead>
                        <tr role="row">
                            <th class="text-left">Ф.И.О.</th>
                            <th class="text-left">Login</th>
                            <th class="text-left">Активен</th>
                            <th class="text-left">Skype</th>
                            <th class="text-left">Телефон</th>
                            <th class="text-left">HR</th>
                            <th class="text-left">Менеджер</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
            </table>
            
            <script type="text/javascript">
            
            function renderCheckmark(data,type,row) {
                if (data) return '<i class="icon-checkmark"></i>';
                else return '<i class="icon-cancel-2"></i>';
            }
            
            $(document).ready(function() {

                document.getElementById('su_set_rights').disabled = true;
                document.getElementById('su_block_user').disabled = true;

                $('#select_user_table').dataTable({
                        "bFilter":false,
                        "bServerSide":true,
                        "sAjaxSource": "./a_select_user",
                        "oLanguage": {
                            "sUrl": "./resources/datatables.russian.txt"
                        },
                        "aoColumns": [
                            {"mData":"nameSurnamePatronymic"},
                            {"mData":"login"},
                            {"mData":"active", "bSortable":false, "sClass":'text-center',
                            "mRender": function(data,type,row){
                                if (data) return '<input type="hidden" value="true"><i class="icon-checkmark"></i>';
                                else return '<input type="hidden" value="false"><i class="icon-cancel-2"></i>';
                            }},
                            {"mData":"skype", "bSortable":false},
                            {"mData":"phone", "bSortable":false},
                            {"mData":"hr", "bSortable":false, "sClass":'text-center',
                            "mRender": function(data,type,row){
                                if (data) return '<i class="icon-checkmark"></i>';
                                else return '<i class="icon-cancel-2"></i>';
                            }},
                            {"mData":"manager", "bSortable":false, "sClass":'text-center',
                            "mRender": function(data,type,row){
                                if (data) return '<i class="icon-checkmark"></i>';
                                else return '<i class="icon-cancel-2"></i>';
                            }}
                        ],
                        "fnRowCallback": function(nRow, aData, iDisplayIndex) {
                            nRow.onclick = function(e) {
                                $('#select_user_table tbody tr').removeClass('row_selected');
                                
                                var pNode = $(e.target).closest('tr');
                                
                                //console.log(pNode);
                                
                                $(pNode).toggleClass('row_selected');
                                
                                blockButtons(false);
                                
                                var userActive = (pNode[0].cells[2].children[0].value === 'true');
                                
                                console.log(userActive);
                                
                                displayActivationButton(userActive);
                            };
                        }   
                    });
            });
            
            function blockButtons(block) {
                document.getElementById('su_block_user').disabled = block;
                document.getElementById('su_set_rights').disabled = block;
            }
            
            function setIconActive(userId, newActive) {
                displayActivationButton(newActive);
                var cellUserActive = document.getElementById(userId).childNodes[2];
                if (newActive) cellUserActive.innerHTML = 
                   '<input type="hidden" value="true"><i class="icon-checkmark"></i>';
                else cellUserActive.innerHTML =
                   '<input type="hidden" value="false"><i class="icon-cancel-2"></i>';
            }
            
            function displayActivationButton(userActive) {
                if (userActive) {
                        document.getElementById('su_block_user').innerHTML = 
                        '<i class="icon-plus on-left"></i>Блокировать пользователя';
                        document.getElementById('su_set_rights').disabled = false;
                } else {
                        document.getElementById('su_block_user').innerHTML = 
                        '<i class="icon-plus on-left"></i>Разблокировать пользователя';
                        document.getElementById('su_set_rights').disabled = true;
                }
            }
            
            function blockUser() {
                var selectedRow = $('#select_user_table tr.row_selected');
                //console.log(selectedRow);
                var userId = selectedRow[0].id;
                //console.log(selectedRow.childNodes[2].childNodes[0].tagName);
                var isActive = (selectedRow[0].cells[2].children[0].value === 'true');
                var act = (isActive) ? 0 : 1;
                if (userId !== null) {
                    $.ajax({
                       "url":"a_activate_user",
                       "type": 'POST',
                       "data": {"id":userId,"active":act},
                       "success" : function() {
                            $.Notify({
                               "content":"Изменения приняты.",
                               "timeout": 5000
                            });
                            setIconActive(userId,!isActive);
                        }
                    });
                }
            }
            
            function setIcons(rowId, iHr, iManager) {
                var row = document.getElementById(rowId);
                
                var cellHr = row.childNodes[5];
                var cellMan = row.childNodes[6];
                if (iHr) cellHr.innerHTML = '<i class="icon-checkmark"></i>';
                else cellHr.innerHTML = '<i class="icon-cancel-2"></i>';
                if (iManager) cellMan.innerHTML = '<i class="icon-checkmark"></i>';
                else cellMan.innerHTML = '<i class="icon-cancel-2"></i>';
            }
           
           
           
           
           
           
        </script>
        </div>
        </tiles:putAttribute>
        </tiles:insertDefinition>